﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace z.Controllers {
	[z.Filters.InitializeSimpleMembership]
	public class Order_DetailController : Controller {

		public ActionResult Index() {
			return View();
		}
		public ActionResult All(int ID) {
			var m = App.db.Orders.Where(x => x.ID == ID).Single();
			return View(m);
		}
		[Authorize]
		public ActionResult _Add(int ParentID) {
			var order = App.db.Orders.Where(x => x.ID == ParentID).Single();
			if ((order.Customer.UserName != User.Identity.Name || order.Pay_Date.HasValue)
				&& !User.IsInRole("Admin"))
				return null;
			var m = new m.db.Order_Detail();
			m.Order_ID = ParentID;
			ViewData.Add("readonly", false);
			ViewData.Add("add", true);
			ViewData.Add("delete", true);
			return PartialView("_Row", m);
		}
		[HttpPost]
		[Authorize]
		public ActionResult _Delete(int ID) {
			var m = App.db.Order_Details.Where(x => x.ID == ID).Single();
			var order = m.Order;
			if ((order.Customer.UserName != User.Identity.Name || order.Pay_Date.HasValue)
				&& !User.IsInRole("Admin"))
				return null;
			App.db.Order_Details.Remove(m);
			App.db.SaveChanges();
			WebSocketController.SendMessenger("Order_Detail," + m.ID);
			return PartialView("Null");
		}
		[HttpPost]
		[Authorize]
		public ActionResult _Edit(m.db.Order_Detail In) {
			App.resetDB();
			ViewData.Add("readonly", false);
			ViewData.Add("add", true);
			ViewData.Add("delete", true);
			var order = App.db.Orders.Where(x => x.ID == In.Order_ID).Single();
			if ((order.Customer.UserName != User.Identity.Name || order.Pay_Date.HasValue)
				&& !User.IsInRole("Admin"))
				return null;
			var isAdd = App.db.Order_Details.Find(In.ID) == null;
			var m = isAdd ? App.db.Order_Details.Add(new m.db.Order_Detail())
				: App.db.Order_Details.Where(x => x.ID == In.ID).Single();
			if (App.db.Products.Where(x => x.ID == In.Product_ID).Count() == 0) {
				return null;
			}
			m.Product = App.db.Products.Where(x => x.ID == In.Product_ID).Single();
			m.Unit_Price = m.Product.Price;
			m.Order_ID = In.Order_ID;
			m.Quantity = In.Quantity;
			if (isAdd) {
				App.db.Order_Details.Add(m);
			}
			ModelState["Product.Name"].Errors.Clear();
			ModelState["Product.Price"].Errors.Clear();
			if (ModelState.IsValid) {
				try {
					App.db.SaveChanges();
				} catch (Exception ex) {
					return null;
				}
			} else {
				return null;
			}
			WebSocketController.SendMessenger("Order_Detail," + m.ID);
			return PartialView("_Row", m);
		}
		[Authorize]
		public ActionResult _Show(int ID) {
			var order_detail = App.db.Order_Details.Where(x => x.ID == ID).Single();
			if (User.IsInRole("Admin") || (User.Identity.Name == order_detail.Order.Customer.UserName && !order_detail.Order.Pay_Date.HasValue)) {
				ViewData.Add("readonly", false);
				ViewData.Add("add", true);
				ViewData.Add("delete", true);
			} else if (User.IsInRole("Mod")) {
				ViewData.Add("readonly", true);
			}
			return PartialView("_Row", order_detail);
		}
		public ActionResult Administer(int ID) {
			ViewData.Add("readonly", false);
			ViewData.Add("add", true);
			ViewData.Add("delete", true);
			return View("Dialog", App.db.Orders.Where(x => x.ID == ID).Single());
		}
	}
}
